#ifndef SPINLOCK_H
#define SPINLOCK_H

#include <atomic>

class CSpinLock
{
public:
    CSpinLock() : flag(false) {}
    ~CSpinLock() {}

    void lock()
    {
        bool expect = false;

        while (!flag.compare_exchange_weak(expect, true))
        {
            expect = false;
            //asm volatile("pause\n" ::: "memory");
            _mm_pause();
        }
    }

    void unlock()
    {
        flag.store(false);
    }

private:
    std::atomic<bool> flag;
};

#endif // SPINLOCK_H
